#! /bin/bash
#
#Authors:
#  Bruno Goncalves Araujo <www.biglinux.com.br>
#
#License: GPLv2 or later
#################################################

. /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)

export TEXTDOMAINDIR="/usr/share/locale-langpack"
export TEXTDOMAIN=bigcontrolcenter-others-bigconvert

#############
## LEGENDA
#############

if [ "$(cat ~/.bigconvert/bordalegenda)" = "yes" ]
then
    bordalegenda=1
else
    bordalegenda=0
fi


if [ "$(cat ~/.bigconvert/embutirlegenda)" = "yes" ]
then
    if [ -e "$(echo "$1" | sed ':a;$ !{N;ba;};s/\(.*\)\..*/\1.ass/')" ]
    then
	legenda="-ass -sub \"$(echo "$1" | sed ':a;$ !{N;ba;};s/\(.*\)\..*/\1.ass/')\" -ass-color $(cat ~/.bigconvert/corlegenda)00 -ass-font-scale $(cat ~/.bigconvert/fontsize) -ass-force-style FontName=FreeSans,Default.Bold=$bordalegenda"
    else
	if [ -e "$(echo "$1" | sed ':a;$ !{N;ba;};s/\(.*\)\..*/\1.srt/')" ]
	then
	    legenda="-ass -sub \"$(echo "$1" | sed ':a;$ !{N;ba;};s/\(.*\)\..*/\1.srt/')\" -ass-color $(cat ~/.bigconvert/corlegenda)00 -ass-font-scale $(cat ~/.bigconvert/fontsize) -ass-force-style FontName=FreeSans,Default.Bold=$bordalegenda"
	fi
    fi
fi



########
## AUDIO
########

if [ "$(cat /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)  | grep channel=\"yes\")" != "" ]
then
    if [ "$(cat ~/.bigconvert/canaisaudio)" = "1" ]
    then
	channel="channels=$(cat ~/.bigconvert/canaisaudio):mode=3"
    else
        channel="channels=$(cat ~/.bigconvert/canaisaudio)"
    fi
fi


if [ "$(cat ~/.bigconvert/trilhaaudio)" = "auto" ]
then
    trilhaaudio=""
else
    trilhaaudio="-aid $(cat ~/.bigconvert/trilhaaudio)"
fi

if [ "$(cat ~/.bigconvert/volumeaudio)" = "0" ]
then
    volumeaudio=""
else
    volumeaudio="volume=$(cat ~/.bigconvert/volumeaudio)"
fi

if [ "$(cat /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)  | grep video=\"no\")" != "" ]
then
    rawaudio="-of rawaudio"
else
    rawaudio=""
fi

if [ "$(cat ~/.bigconvert/canaisaudio)" = "0" ]
then
    audio="-nosound"
else
    audio="$(sed "s/\$audio/$(cat ~/.bigconvert/qualidadeaudio)/g" /usr/share/bigcontrolcenter/categories/others/bigconvert/mencoder-audio/$(cat ~/.bigconvert/audioformat).sh.htm) $trilhaaudio -af $volumeaudio $channel $rawaudio"
fi





########
## VIDEO
########


if [ "$(cat /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)  | grep video=\"yes\")" = "" ]
then
    video="-ovc frameno"
else
    if [ "$(cat ~/.bigconvert/turbo)" = "yes" ]
    then
	video="$(sed "s/\$bitrate/$(cat ~/.bigconvert/qualidadevideo)/g" /usr/share/bigcontrolcenter/categories/others/bigconvert/mencoder-video/turbo$(cat ~/.bigconvert/formatsimples))"
    else
	video="$(sed "s/\$bitrate/$(cat ~/.bigconvert/qualidadevideo)/g" /usr/share/bigcontrolcenter/categories/others/bigconvert/mencoder-video/$(cat ~/.bigconvert/formatsimples))"
    fi
fi

################
## REDIMENSIONAR
################

if [ "$(cat ~/.bigconvert/resize)" = "0" ]
then
    redimensionar=""
else

if [ "$(grep xvid ~/.bigconvert/formatsimples)$(grep 3gp ~/.bigconvert/formatsimples)$(grep mp4mpeg ~/.bigconvert/formatsimples)$(grep divx ~/.bigconvert/formatsimples)$(grep flv ~/.bigconvert/formatsimples)" != "" ]
then

    if [ "$(cat ~/.bigconvert/resize)" = "manual" ]
    then
	redimensionar=",scale=$(cat ~/.bigconvert/manual-largura):-3"
    else
	redimensionar=",scale=$(sed 's/x.*//g' ~/.bigconvert/resize):-3"
    fi


else

    if [ "$(cat ~/.bigconvert/resize)" = "manual" ]
    then
	redimensionar=",scale=$(cat ~/.bigconvert/manual-largura):$(cat ~/.bigconvert/manual-altura)"
    else
	redimensionar=",scale=$(sed 's/x/:/g' ~/.bigconvert/resize)"
    fi

fi

    if [ "$(cat ~/.bigconvert/resize)" = "dvd" ]
    then
	if [ "$(mediainfo --Inform="Video;%Width%" "$1")" -gt "720" ]
	then
	    redimensionar=",scale=720:-3"
	else
	    redimensionar=""
	fi
    fi

fi

################
## FPS
################


if [ "$(cat ~/.bigconvert/fps)" = "" ]
then
    fps=""
else
if [ "$(cat ~/.bigconvert/fps)" = "0" ]
then
    fps=""
else
	fps="-ofps $(cat ~/.bigconvert/fps)"
    fi
fi

####################################
## EXTRAS BRILHO CONTRASTE SATURACAO
####################################

extras="-vf harddup,eq2=1:$(cat ~/.bigconvert/contraste):$(cat ~/.bigconvert/brilho):$(cat ~/.bigconvert/saturacao)"


########
## SAIDA
########

echo "echo \"$1\"" > ~/.bigconvert/bigdestino
echo " | sed ':a;$ !{N;ba;};s/\(.*\)\..*/\1.avi/'" >> ~/.bigconvert/bigdestino
sed -i "s/.avi/$(cat /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)  | grep extensao | sed 's/extensao="//g;s/"//g')/g" ~/.bigconvert/bigdestino
sed -i 's/$ !/$!/g;:a;$!N;s/\n//;ta;' ~/.bigconvert/bigdestino
chmod +x ~/.bigconvert/bigdestino

saida="-o \"$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")\""


###########
## EXECUTAR
###########


if [ "$(cat /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)  | grep 2pass=\"yes\")" != "" ]
then

  if [ "$2" = "prever" ]
  then

  if [ "$(mediainfo --Inform="Video;%Duration/String3%" "$1" | sed 's/\..*//g;s/://g')" -gt "$(cat ~/.bigconvert/prevhora)$(cat ~/.bigconvert/prevminuto)$(cat ~/.bigconvert/prevsegundo)" ]
  then
    inicioprev="$(cat ~/.bigconvert/prevhora):$(cat ~/.bigconvert/prevminuto):$(cat ~/.bigconvert/prevsegundo)"
  else
    inicioprev="00:00:00"
    kdialog --passivepopup $"Tempo indicado para iniciar a previsao ultrapassa o limite do video, gerando previsao a partir do inicio." --title "Big Converter"
  fi

    echo "mencoder \"$1\" -ss $inicioprev -endpos $(cat ~/.bigconvert/prevduracao) $legenda $fps $video $extras$redimensionar -o ~/.bigconvert/videoprevisto.$passvideo" > ~/.bigconvert/mencoderprev

#Mudo ou nao
    if [ "$(cat ~/.bigconvert/canaisaudio)" = "0" ]
    then
	echo "MP4Box -new -fps $(mediainfo "'--Inform="Video;%FrameRate%"'" ~/.bigconvert/videoprevisto.$passvideo) -add ~/.bigconvert/videoprevisto.$passvideo ~/.bigconvert/videoprevisto" >> ~/.bigconvert/mencoderprev
    else
	echo "mencoder \"$1\" -ss $inicioprev -endpos $(cat ~/.bigconvert/prevduracao) $audio -o ~/.bigconvert/videoprevisto.$passaudio" >> ~/.bigconvert/mencoderprev
	echo 'MP4Box -new -fps $(mediainfo --Inform="Video;%FrameRate%" ~/.bigconvert/videoprevisto.'"$passvideo"')'" -add ~/.bigconvert/videoprevisto.$passvideo -add ~/.bigconvert/videoprevisto.$passaudio ~/.bigconvert/videoprevisto" >> ~/.bigconvert/mencoderprev
    fi

  else
    echo "mencoder \"$1\" $legenda $fps $video $extras$redimensionar $saida.$passvideo" > ~/.bigconvert/mencoder

    #Mudo ou nao
    if [ "$(cat ~/.bigconvert/canaisaudio)" = "0" ]
    then
	echo 'MP4Box -new -fps $(mediainfo --Inform="Video;%FrameRate%" "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passvideo"'") -add "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passvideo"'" "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g" | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")"' >> ~/.bigconvert/mencoder
    else
	echo "mencoder \"$1\" $audio $saida.$passaudio" >> ~/.bigconvert/mencoder
	echo 'MP4Box -new -fps $(mediainfo --Inform="Video;%FrameRate%" "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passvideo"'") -add "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passvideo"'" -add "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passaudio"'" "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g" | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")"' >> ~/.bigconvert/mencoder
    fi
    echo 'rm -f "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passaudio"'"' >> ~/.bigconvert/mencoder
    echo 'rm -f "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passvideo"'"' >> ~/.bigconvert/mencoder

#echo 'MP4Box -new -delay 2=$[$[$(mediainfo --Inform="Video;%Duration%" "'"$1"'") - $(mediainfo --Inform="Audio;%Duration%" "'"$1"'")] + $[$(mediainfo --Inform="Video;%Duration%" "'"$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")"'") - $(mediainfo --Inform="Audio;%Duration%" "'"$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")"'")]] -fps $(mediainfo "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passvideo"'" | grep "Frame rate" | sed "s/.*: //g;s/ .*//g") -add "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passvideo"'" -add "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g").'"$passaudio"'" "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g" | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")"' >> ~/.bigconvert/mencoder
  fi

else


  if [ "$2" = "prever" ]
  then
  if [ "$(mediainfo --Inform="Video;%Duration/String3%" "$1" | sed 's/\..*//g;s/://g')" -gt "$(cat ~/.bigconvert/prevhora)$(cat ~/.bigconvert/prevminuto)$(cat ~/.bigconvert/prevsegundo)" ]
  then
    inicioprev="$(cat ~/.bigconvert/prevhora):$(cat ~/.bigconvert/prevminuto):$(cat ~/.bigconvert/prevsegundo)"
  else
    inicioprev="00:00:00"
    kdialog --passivepopup $"Tempo indicado para iniciar a previsao ultrapassa o limite do video, gerando previsao a partir do inicio." --title "Big Converter"
  fi

    echo "mencoder \"$1\" -ss $inicioprev -endpos $(cat ~/.bigconvert/prevduracao) $legenda $audio $fps $video $extras$redimensionar -o ~/.bigconvert/videoprevisto" > ~/.bigconvert/mencoderprev
    #Remendo para M4A
    if [ "$(cat /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)  | grep "Audio M4A")" != "" ]
    then
	echo 'MP4Box -new -add "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")" "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g;s|.aac$|.m4a|g")"' >> ~/.bigconvert/mencoder
    fi
  else
    echo "mencoder \"$1\" $legenda $audio $fps $video $extras$redimensionar $saida" > ~/.bigconvert/mencoder
    #Remendo para M4A
      if [ "$(cat /usr/share/bigcontrolcenter/categories/others/bigconvert/simples/$(cat ~/.bigconvert/formatsimples)  | grep "Audio M4A")" != "" ]
      then
	echo 'MP4Box -new -add "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g")" "$(~/.bigconvert/bigdestino | sed "s|.*/|$(cat ~/.bigconvert/save)/|g;s|.aac$|.m4a|g")"' >> ~/.bigconvert/mencoder
      fi
  fi
fi